import gather from './gather'
import { typeName } from './util/common'
/**
 *
 *
 * @class Moon
 */
class Moon {

  static _MOON_ = '_moon_';
  static systemData;
  static request;
  static mid;
  static domain;
  static env;
  static reqType;
  static spa;
  static userId;
  static logUrl;
  static dataKey;
  static extra;
  static gather;
  static storage;
  static initPuv;
  /**
 * 添加配置
 * @static
 * @type {any} mid, domain, env, reqType, spa, systemData, request, userId, _MOON_
 * @memberof Moon
 */
  static init(config: any) {
    if (Moon.env) return;
    Moon.mid = config.mid;
    Moon.domain = config.domain;
    Moon.env = config.env;
    Moon.reqType = config.reqType;
    Moon.spa = config.spa;
    Moon.logUrl = `${Moon.domain}/rlcas/${Moon.mid}`;
    Moon.dataKey = `${Moon._MOON_}${Moon.mid}`
    Moon.gather = gather(Moon);
    Moon.initPuv();
  }

  static baseData() {
    return {
      gid: Moon.userId(Moon.dataKey),
      env: Moon.env,
      ...Moon.systemData()
    }
  }

  static async getData(data) {
    if (!Moon.env) return;
    if (!data) return;
    for (let i in data) {
      if (data[i] === '' || typeof data[i] === 'undefined' || data[i] === null) delete data[i]
    }
    data = {
      ...Moon.baseData(),
      ...data
    };
    const extra = Moon.extra;
    if (typeName(extra) === 'function') {
      data = await Moon.extra(data)
    } else if (typeName(extra) === 'object') {
      data = {
        ...data,
        ...extra,
      }
    } else if (Moon.extra) {
      data.moonextra = extra;
    }
    return data;
  }
  /**
   *
   *
   * @static
   * @param {*} data
   * @param {string} type  http, image
   * @returns
   * @memberof Moon
   */
  static async logger(data, type?) {
    const _type = type || Moon.reqType;
    const _data = await Moon.getData(data);
    return _type == 'http' ? Moon.gather(_data) : Moon.request(Moon.logUrl, _data, 'get');
  }

  static error(err) {
    if (err.constructor && err.constructor.name.toLocaleLowerCase().indexOf('error') > -1) {
      return Moon.logger({
        name: err.name,
        stack: err.stack,
        message: err.message,
        t: 'logdebug_js'
      })
    }
  }

  static oper(obj: any, code: string = "error") {
    let data: any = {}
    if (!obj.config) obj.config = {};
    if (obj.constructor && obj.constructor.name.toLocaleLowerCase().indexOf('error') > -1) {
      data = {
        url: obj.config.url,
        header: obj.config.headers,
        data: obj.config.data,
        params: obj.config.params,
        stack: obj.stack,
        t: 'logdebug_op',
        code: code,
        message: obj.message
      }
      if (obj.obj) {
        data.stack = obj.obj.status
        data.message = obj.obj.data
      }
      return Moon.logger(data)
    }
    return Moon.logger({
      url: obj.config.url,
      t: 'logdebug_op',
      code: code,
      header: obj.config.headers,
      data: obj.config.data,
      params: obj.config.params,
      res: obj.data
    })
  }

  static logUser(user: any): Promise<any> {
    return Moon.logger({
      t: 'userlog', // userlog
      u: user
    })
  }
  static logdebug(data) {
    const _data = {
      env: Moon.env,
      xt_logdata: data
    }
    Moon.request(Moon.logUrl + '/logdebug', _data, 'post')
  }

  static flowname(flowname: string): object {
    if (!flowname) return;
    if (flowname.indexOf('~') > 0) return { flowname };
    return { flowname: flowname + '~' + Math.random().toString().substr(2, 6) }

  }
}


export default Moon;


